Introduction

Column

Motivation

Carbon Emissions: Which nations are contributing the most to Climate Change?

With the annual reports of anthropogenic climate change being more dire, and that carbon emissions need to be reduced, I found it important to see who was contributing the most and whether those reductions were being met.

Some of the research questions for this project:

  1. Which nations have contributed the most to climate change in terms of CO2 emissions?

  2. What are the factors that have led to such high emission rates?

  3. Which nations, if any, have reduced their emissions?

Column

All of the data for this project was obtained from Our World in Data.

We will be looking at data between 1949 and 2019.

Variables in Dataset

The following are some of the variables:

-Country

-Year

-Total Emissions

-Trade Emissions

-Consumption Emissions

-Production Emissions

-Emissions by Income

Modifications in R

The data from OWID begins in 1750 and only contains 223 countries. From 1949 onward, there are 224 countries included in the dataset.In order to have more consistentcy, the data files were modified to begin in 1949 so all emissions will be counted from there. The dataset also previously accounted for total emissions, but for the purposes of this analysis, the emissions data were modified to account for the average both per year and all-time.

Data Exploration

Column

Some figures

You can put figure(s) here!

Column

Some explanations regarding the figures you have

Diagnostics

Column

Linearity

Normality

Equality Variance

Residuals vs Leverage

Column

First, we load the necessary packages and data. Fit a linear model to the data and obtain necessary objects for diagnostics plots.

1. Linearity Assumption

We check the linearity assumption: there is a linear relationship between…

2. Normality Assumption

We check the normality assumption:….

Section 4

Column

Figure on the first row and first column

Figure on the second row and the first column

Column

Figure on the first row and the second column

Figure on the second row and the second column

Section 5

Row

Another section

Put something here!

Row

Put something here!

Put something in this area!

Row 1

Put something in row 1.

Row 2

Put something in row 2.

Tableau

---
title: "CO2 Emissions Dashboard"
author: "Anthony Lapham"
output: 
  flexdashboard::flex_dashboard:
    theme: paper
    orientation: columns
    social: ["linkedin"]
    source_code: embed
---

```{r setup, include=FALSE}
# load necessary packages
library(ggplot2)
library(plotly)
library(plyr)
library(readxl)
library(flexdashboard)  ## you need this package to create dashboard

# read the data set here, I use data: mtcars as an example
df <- mtcars
```

Introduction
=======================================================================


Column {.tabset data-width=800} 
-----------------------------------------------------------------------

### Motivation


Carbon Emissions: Which nations are contributing the most to Climate Change?

With the annual reports of anthropogenic climate change being more dire, and that carbon emissions need to be reduced, I found it important to see who was contributing the most and whether those reductions were being met.

Some of the research questions for this project:

1. Which nations have contributed the most to climate change in terms of CO2 emissions?

2. What are the factors that have led to such high emission rates?

3. Which nations, if any, have reduced their emissions?




Column {.tabset data-width=800}
-----------------------------------------------------------------------


All of the data for this project was obtained from Our World in Data.

We will be looking at data between 1949 and 2019.



### Variables in Dataset


The following are some of the variables:

-Country

-Year

-Total Emissions

-Trade Emissions

-Consumption Emissions

-Production Emissions

-Emissions by Income




### Modifications in R 



The data from OWID begins in 1750 and only contains 223 countries. From 1949 onward, there are 224 countries included in the dataset.In order to have more consistentcy, the data files were modified to begin in 1949 so all emissions will be counted from there. The dataset also previously accounted for total emissions, but for the purposes of this analysis, the emissions data were modified to account for the average both per year and all-time.




Data Exploration
=======================================================================

Column {data-width=500}
-----------------------------------------------------------------------

### Some figures
You can put figure(s) here!

Column {data-width=500}
-----------------------------------------------------------------------
### Some explanations regarding the figures you have

Diagnostics
=======================================================================
Column {.tabset data-width=650}
-----------------------------------------------------------------------

```{r}
df <- mtcars
df$am <- as.factor(df$am)
fit <- lm(mpg ~ wt + disp + am, data=df)

#obtain values needed in order to get diagnostics plots
# Extract fitted values
Fitted.Values <- fit$fitted.values

# Extract residuals
Residuals <- fit$residuals

# Calculate standardized residuals 
Standardized.Residuals <- scale(fit$residuals)

# Extract fitted values for lm() object
Theoretical.Quantiles <- qqnorm(Residuals, plot.it = F)$x

# find Square root of abs(residuals)
Root.Residuals <- sqrt(abs(Standardized.Residuals))

# Calculate Leverage
Leverage <- lm.influence(fit)$hat

# Create data frame 
# Will be used as input to plot_ly

diagnostics <- data.frame(Fitted.Values, 
                     Residuals, 
                     Standardized.Residuals, 
                     Theoretical.Quantiles,
                     Root.Residuals,
                     Leverage)
```
### Linearity

```{r}
m <- list(
  l = 100,
  r = 100,
  b = 100,
  t = 100,
  pad = 4
)

# Fitted vs Residuals
p1 <- plot_ly(diagnostics, x = Fitted.Values, y = Residuals, 
          type = "scatter", mode = "markers", hoverinfo = "x+y", name = "Data", 
          marker = list(size = 10, opacity = 0.5))%>%
  layout(title = "Residuals vs Fitted Values", 
       xaxis = list(title="Fitted Values", font=list(size=14)), 
       yaxis = list(title="Residuals", font=list(size=14)), 
       plot_bgcolor = "#e6e6e6", 
       font=list(size=14), margin=m)

ggplotly(p1)
```

### Normality
```{r}
# QQ Pot
p2 <- plot_ly(diagnostics, x = Theoretical.Quantiles, y = Standardized.Residuals, type = "scatter", mode = "markers", hoverinfo = "x+y", name = "Data", marker = list(size = 10, opacity = 0.5), showlegend = F)%>%
    add_trace(x = Theoretical.Quantiles, y = Theoretical.Quantiles, type = "scatter", mode = "line", name = "", line = list(width = 2))%>%
  layout(title = "Q-Q Plot", plot_bgcolor = "#e6e6e6",
              xaxis = list(title="Theoretical Quantiles", font=list(size=14)), 
       yaxis = list(title="Standardized Residuals", font=list(size=14)), font=list(size=14), margin=m)

ggplotly(p2)
```

### Equality Variance
```{r}
# Scale Location
p3 <- plot_ly(diagnostics, x = Fitted.Values, y = Root.Residuals, 
          type = "scatter", mode = "markers", hoverinfo = "x+y", name = "Data",
          marker = list(size = 10, opacity = 0.5), showlegend = F)%>%
  layout(title = "Scale-Location", plot_bgcolor = "#e6e6e6", xaxis = list(title="Fitted Values", font=list(size=14)), 
       yaxis = list(title=expression(sqrt("|Standardized Residuals|")), font=list(size=14)), font=list(size=14), margin=m)

ggplotly(p3)
```

### Residuals vs Leverage
```{r}
s <- loess.smooth(Leverage, Residuals)
p4 <- plot_ly(diagnostics, x = Leverage, y = Residuals, 
            type = "scatter", mode = "markers", hoverinfo = "x+y", name = "Data", marker = list(size = 10, opacity = 0.5), showlegend = F) %>% 
    add_trace(x = s$x, y = s$y, type = "scatter", mode = "line", name = "Smooth", line = list(width = 2)) %>% 
    layout(title = "Leverage vs Residuals", plot_bgcolor = "#e6e6e6", xaxis = list(title="Leverage", font=list(size=14)), 
       yaxis = list(title="Residuals", font=list(size=14)), font=list(size=14), margin=m)
ggplotly(p4)  
```


Column {data-width=350}
-----------------------------------------------------------------------
 
First, we load the necessary packages and data. Fit a linear model to the data and obtain necessary objects for diagnostics plots. 

**1. Linearity Assumption**

We check the linearity assumption: there is a linear relationship between... 

**2. Normality Assumption**

We check the normality assumption:....


Section 4 
=======================================================================

Column
-----------------------------------------------------------------------

### Figure on the first row and first column

```{r}
dfGamma = data.frame(nu75 = rgamma(100, 0.75),
           nu1 = rgamma(100, 1),
           nu2 = rgamma(100, 2))

dfGamma = stack(dfGamma)

p <- ggplot(dfGamma, aes(x = values)) +
            stat_density(aes(group = ind, color = ind),position="identity",geom="line")
ggplotly(p)
```

### Figure on the second row and the first column

```{r}
x <- rnorm(100)
hist(x)
```

Column
-----------------------------------------------------------------------

### Figure on the first row and the second column
```{r}
dd<-data.frame(matrix(rnorm(144, mean=2, sd=2),72,2),c(rep("A",24),rep("B",24),rep("C",24)))
colnames(dd) <- c("x_value", "Predicted_value",  "State_CD")

dd <- data.frame(
  predicted = rnorm(72, mean = 2, sd = 2),
  state = rep(c("A", "B", "C"), each = 24)
)

grid <- with(dd, seq(min(predicted), max(predicted), length = 100))
normaldens <- ddply(dd, "state", function(df) {
  data.frame(
    predicted = grid,
    density = dnorm(grid, mean(df$predicted), sd(df$predicted))
  )
})

p <- ggplot(dd, aes(predicted))  +
            geom_density() +
            geom_line(aes(y = density), data = normaldens, colour = "red") +
            facet_wrap(~ state)
ggplotly(p)
```

### Figure on the second row and the second column

```{r}
df <- data.frame(x <- rchisq(1000, 10, 10),
                 y <- rnorm(1000))

p <- ggplot(df, aes(x, y)) + 
     geom_point(alpha = 0.5) + 
     geom_density_2d() + 
     theme(panel.background = element_rect(fill = '#ffffff'))

ggplotly(p)
```

Section 5
=======================================================================

Row {data-hight=650}
-----------------------------------------------------------------------
### Another section
Put something here!

Row {data-height=350}
-----------------------------------------------------------------------

### Put something here!
Put something in this area!

### Row 1
Put something in row 1.

### Row 2
Put something in row 2.

Tableau
======================================================================